package com.mano233.server.controller;

import com.mano233.server.config.security.UserCustomDetails;
import com.mano233.server.po.ResponseMessage;
import com.mano233.server.po.ResponseStatus;
import com.mano233.server.service.serviceImpl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api
@CrossOrigin(maxAge = 3600)
public class UserController {
    private final UserServiceImpl userService;

    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @ApiOperation("获取用户所有填写的表单")
    @GetMapping("/user/form")
    public ResponseMessage getUserFormEditorList(Authentication authentication) {
        UserCustomDetails details = (UserCustomDetails) authentication.getPrincipal();
        return new ResponseMessage(ResponseStatus.SUCCESS, userService.getUserFormEditorList(details.getUserId()));
    }

    @ApiOperation("搜索用户")
    @GetMapping("/user/search")
    public ResponseMessage searchUser(String userName) {
        if (userName.isEmpty()) {
            return new ResponseMessage(ResponseStatus.SUCCESS, null);
        }
        return new ResponseMessage(ResponseStatus.SUCCESS, userService.searchUser(userName));
    }
}
